{% extends 'admin/admin_base.html' %}

{% block title %}试题管理 - 后台管理系统{% endblock %}

{% block nav_questions %}active{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>试题管理</h2>
    <a href="{{ url_for('admin.question_create') }}" class="btn btn-primary">
        创建试题
    </a>
</div>

<div class="card mb-4">
    <div class="card-body">
        <form method="get" class="row g-3">
            <div class="col-md-4">
                <input type="text" class="form-control" name="search" placeholder="搜索试题..." value="{{ search }}">
            </div>
            <div class="col-md-3">
                <select class="form-select" name="type">
                    <option value="">所有类型</option>
                    <option value="single" {% if question_type == 'single' %}selected{% endif %}>单选题</option>
                    <option value="multiple" {% if question_type == 'multiple' %}selected{% endif %}>多选题</option>
                    <option value="boolean" {% if question_type == 'boolean' %}selected{% endif %}>判断题</option>
                </select>
            </div>
            <div class="col-auto">
                <button type="submit" class="btn btn-secondary">搜索</button>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>题目</th>
                        <th>类型</th>
                        <th>选项数</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for question in questions %}
                    <tr>
                        <td>{{ question.title }}</td>
                        <td>
                            {% if question.type == 'single' %}
                            <span class="badge bg-primary">单选题</span>
                            {% elif question.type == 'multiple' %}
                            <span class="badge bg-success">多选题</span>
                            {% else %}
                            <span class="badge bg-info">判断题</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if question.type in ['single', 'multiple'] %}
                            {{ question.options|length }}
                            {% else %}
                            -
                            {% endif %}
                        </td>
                        <td>
                            <div class="btn-group" role="group">
                                <a href="{{ url_for('admin.question_edit', question_id=question._id) }}" 
                                   class="btn btn-sm btn-outline-primary">编辑</a>
                                <button type="button" class="btn btn-sm btn-outline-danger" 
                                        onclick="confirmDelete('{{ question._id }}', '{{ question.title }}')">删除</button>
                            </div>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="4" class="text-center">暂无试题数据</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        {% if total_pages > 1 %}
        <nav aria-label="Page navigation" class="mt-4">
            <ul class="pagination justify-content-center">
                <li class="page-item {% if current_page == 1 %}disabled{% endif %}">
                    <a class="page-link" href="{{ url_for('admin.question_list', page=current_page-1, search=search, type=question_type) }}">上一页</a>
                </li>
                
                {% for page in range(1, total_pages + 1) %}
                <li class="page-item {% if page == current_page %}active{% endif %}">
                    <a class="page-link" href="{{ url_for('admin.question_list', page=page, search=search, type=question_type) }}">{{ page }}</a>
                </li>
                {% endfor %}
                
                <li class="page-item {% if current_page == total_pages %}disabled{% endif %}">
                    <a class="page-link" href="{{ url_for('admin.question_list', page=current_page+1, search=search, type=question_type) }}">下一页</a>
                </li>
            </ul>
        </nav>
        {% endif %}
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                确定要删除试题 "<span id="deleteQuestionTitle"></span>" 吗？此操作不可恢复！
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteForm" method="post" style="display: inline;">
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function confirmDelete(questionId, title) {
    document.getElementById('deleteQuestionTitle').textContent = title;
    document.getElementById('deleteForm').action = "{{ url_for('admin.question_delete', question_id='') }}" + questionId;
    
    var deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
    deleteModal.show();
}
</script>
{% endblock %}
